home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 February / PCWFEB06.iso / Software / Resources / SyncBack Freeware 3.2.9 / SyncBack_Setup.exe / {app} / RotateBackup.vbs < prev    next >
Text File  |  2004-07-21  |  7KB  |  203 lines

  1. '------------------------------------------------------
  2. ' Save historic versions of the backup, so that you can
  3. ' backtrack to previous versions. This script will, if
  4. ' ran BEFORE the SyncBack-backup, copy the last backup
  5. ' (either directory or zip-file) to a historic version
  6. ' in the same destination directory.
  7. '
  8. ' The historic set consists of folders/files in the
  9. ' backup-directory that have a "1_", "2_" etc prefixed
  10. ' to the original backupfilename or -foldername. After
  11. ' the (in this script configured) maximum number of
  12. ' historic versions is reached, the script starts over
  13. ' at "1_" and repeats the cyclus.
  14. '
  15. ' That way, by looking at the modifation-date of the
  16. ' historic files or folders, you can always find the
  17. ' previous backups. Because of this, you can not
  18. ' automatically assume that the "1_" version is the
  19. ' oldest or newest version, it all depends on the
  20. ' modification-date of the files or folders.
  21. '
  22. ' Created by Alex, based on code from timestamp.vbs as
  23. ' provided by Michael J. Leaver (www.2BrightSparks.com)
  24. '
  25. ' Free for non-commercial use.
  26. '
  27. ' Run by calling "cscript rotate.vbs <arg>"
  28. ' where <arg> is full path of the destination file or
  29. ' folder.
  30. ' Files should be full filenames (path included)
  31. ' and without any wildcards.
  32. ' Folders can either end with a "\" or not.
  33. '
  34. ' Examples (for use in the "Run before profile" field
  35. ' of the Programs-tab (Expert mode) of SyncBack:
  36. ' -- for a file-backup to 'backup.zip' -- :
  37. ' cscript "C:\Program Files\2brightsparks\SyncBack\rotate.vbs" "C:\<backupdir>\backUp.zip"
  38. ' -- for a folder-backup to subfolder 'backup' of the backup-dir -- :
  39. ' cscript "C:\Program Files\2brightsparks\SyncBack\rotate.vbs" "C:\<backupdir>\backup"
  40. '
  41. ' All provided that you created rotate.vbs (with this code as content)
  42. ' in C:\Program Files\2brightsparks\SyncBack\
  43. '
  44. ' By modifying the variable 'nRotationSize', you can
  45. ' determine how many historic versions are stored.
  46. '------------------------------------------------------
  47.  
  48. 'set this variable to the number of historic versions you want to preserve.
  49. 'eg, if set to 7, the last seven daily backups are stored in the backup directory
  50. nRotationSize = 7
  51.  
  52. '------------------------------------------------------
  53. 'Return the pathname portion of a full pathname
  54. '------------------------------------------------------
  55. Function Pathname(FullPath)
  56. Dim x, y
  57. Dim tmpstring
  58.  
  59. x = Len(FullPath)
  60. For y = x to 1 step -1
  61. If Mid(FullPath, y, 1) = "\" Or _
  62. Mid(FullPath, y, 1) = "/" Then
  63. tmpstring = Mid(Fullpath, 1, y-1)
  64. Exit For
  65. End If
  66. Next
  67.  
  68. Pathname = tmpstring
  69.  
  70. End Function
  71.  
  72. '------------------------------------------------------
  73. 'Return the filename portion of a full pathname
  74. '------------------------------------------------------
  75. Function Basename(FullPath)
  76. Dim x, y
  77. Dim tmpstring
  78.  
  79. tmpstring = FullPath
  80. x = Len(FullPath)
  81. For y = x To 1 step -1
  82. If Mid(FullPath, y, 1) = "\" Or _
  83. Mid(FullPath, y, 1) = ":" Or _
  84. Mid(FullPath, y, 1) = "/" Then
  85. tmpstring = Mid(Fullpath, y+1)
  86. Exit For
  87. End If
  88. Next
  89. Basename = tmpstring
  90. End Function
  91.  
  92. '------------------------------------------------------
  93. 'Main code
  94. '------------------------------------------------------
  95.  
  96. Set objArgs = WScript.Arguments
  97. Set fso = CreateObject("Scripting.FileSystemObject")
  98.  
  99. If (objArgs.Count < 1) then
  100. WScript.Echo "No filename or dirname to copy was supplied."
  101. Else
  102. ' Concatenate all the arguments to create on file/dirname
  103. ' This is to avoid problems with spaces in filenames or dirnames.
  104. OldFileDirname = ""
  105. For I = 0 To objArgs.Count - 1
  106. OldFileDirname = OldFileDirname & objArgs.Item(I) & " "
  107. Next
  108. OldFileDirname = Trim(OldFileDirname)
  109.  
  110. 'whether the argument represents a file or a folder, eliminate any trailing "\".
  111. If (Right(OldFileDirname,1) = "\") Then
  112. OldFileDirname = Left(OldFileDirname, Len(OldFileDirname) -1)
  113. End If
  114.  
  115. theFolder = Pathname(OldFileDirname)
  116. Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  117. Set oFolder = objFileSystem.GetFolder(theFolder)
  118.  
  119. lngMaxIndex = 0
  120. lngNewIndex = 0
  121. dtMaxDate = 0
  122.  
  123. 'determine whether user gave file or folder as argument
  124. If fso.FileExists(OldFileDirname) = True Then
  125. blIsFile = True
  126. blIsFolder = False
  127. ElseIf fso.FolderExists(OldFileDirname) = True Then
  128. blIsFile = False
  129. blIsFolder = True
  130. Else
  131. blIsFile = False
  132. blIsFolder = False
  133. End If
  134.  
  135. 'determine whether to loop through files or folders of the directory
  136. If blIsFile = True Then
  137. Set arrLoop = oFolder.Files
  138. ElseIf blIsFolder = True Then
  139. Set arrLoop = oFolder.SubFolders
  140. Else
  141. WScript.Echo "File or directory <" & OldFileDirname & "> not found."
  142. End If
  143.  
  144. 'start loop and determine the next-to-be-used prefix ("1_" or "2_" etc)
  145. For Each FileOrFolder in arrLoop
  146. For i = 1 to nRotationSize
  147. If Basename(FileOrFolder) = i & "_" & Basename(OldFileDirname) Then
  148. 'found index, save it, if max
  149. If FileOrFolder.DateLastModified >= dtMaxDate Then
  150. dtMaxDate = FileOrFolder.DateLastModified
  151. lngMaxIndex = i
  152. End If
  153. Else
  154. 'found non-conforming file, skip
  155. End If
  156. Next
  157.  
  158. If lngMaxIndex > 0 then
  159. 'found max index, increase it by 1 to generate the next index for the backups.
  160. 'The mod-command makes sure that the indices rotate and do not increase forever.
  161. lngNewIndex = (lngMaxIndex Mod nRotationSize) + 1
  162. Else
  163. 'no max index found, start with 1
  164. lngNewIndex = 1
  165. End If
  166. Next
  167.  
  168. If blIsFile = True Then
  169. 'the user gave a file-name as argument
  170. 'create the new filename by pre-pending the counter-index to the original filename
  171. 'This way, the file 'backup.zip' will be saved as, for example, "1_backup.zip"
  172. NewFilename = Pathname(OldFileDirname) & "\" & lngNewIndex & "_" & Basename(OldFileDirname)
  173.  
  174. 'copy the old file to the new file, so that the old file can be overwritten by SyncBackup after this script is finished.
  175. fso.CopyFile OldFileDirname, NewFilename ,true
  176.  
  177. WScript.Echo "File <" & NewFilename & "> created."
  178.  
  179. ElseIf blIsFolder = True Then
  180. 'the user gave a folder-name as argument
  181. 'create the new foldername by pre-pending the counter-index to the original foldername
  182. 'This way, the folder 'backup' will be saved as, for example, "1_backup"
  183. NewFoldername = PathName(OldFileDirname) & "\" & lngNewIndex & "_" & BaseName(OldFileDirname)
  184.  
  185. If fso.FolderExists(NewFoldername) = True Then
  186. Set oFolder = objFileSystem.GetFolder(NewFoldername)
  187. 'do an explicit delete, because the WSH-CopyFolder command doesn't seem to update the DateLastModified-field
  188. 'of the destination-folder (either bug or feature).
  189. 'That causes a problem when trying to find the next rotation-folder based on the DateLastModified-field.
  190. oFolder.Delete()
  191. End If
  192.  
  193. 'copy the original folder to one of the rotating folders
  194. fso.CopyFolder OldFileDirname, NewFoldername, True
  195.  
  196. WScript.Echo "Directory <" & NewFoldername & "> created."
  197.  
  198. Else
  199. WScript.Echo "File or directory <" & OldFileDirname & "> not found."
  200. End If
  201.  
  202. End If
  203.